<?php
namespace printPictures;

class printPictures{
	
	public function postPictures(){
	
			
		$files = glob("model/uploads/*.*");
		$colCnt=0;
		$images = "";
		//$table = '<table>';
				for ($i=0; $i<count($files); $i++)
				    {
				    	if(stristr($files[$i], 'Thumbs.db') == FALSE){
								$images .= '<div class="photoPosts">';
								$num = $files[$i];
						    	$images .= '<a name="'.$i.'" href="#'.$i.'"><img src="'.$num.'" width="150" height="150"/></a>';
								$images .= '<form name="'.$files[$i].'" class="form-inline" action="index.php?attempt" method="post" role="form">
									    	<label class="sr-only" for="Username">Username</label>
									    	<input type="text" class="form-control" name="comments" id="text" placeholder="Type text here!">
									    	<button name="postText" type="submit" value="'.$files[$i].'" class="btn btn-default">Skicka!</button>
									    	</form>';
								$images .= '<div class="comments"'. $comment = $this->getCommentsFromDb($files[$i]) .'</div';
								$images .= '</div>';
						}
				    	
				    }
				//$table .= '</table>';
		
		return $images;
	}
		
	private function getCommentsFromDb($imgName){
		
		$con = mysqli_connect("localhost", "root", "", "benny");
		$comment = "";
		// Check connection
		if (mysqli_connect_errno())
		{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
		}
		
		$result = mysqli_query($con,"SELECT * FROM comments");

		while($row = mysqli_fetch_array($result)){
			if($imgName == $row['Id']){
				$comment .= '<p>'. $row['comments'] .'</p>';
			}
  		}
  		return $comment;
	}	
}

/*if(stristr($files[$i], 'Thumbs.db') == FALSE){
						    
						    $colCnt++;
						    if ($colCnt==1)
						    //$table .= '<tr>';
							$table .= '<div class="photoPosts">';
						   //$table .= '<td>';
						    $num = $files[$i];
						    $table .= '<a name="'.$i.'" href="#'.$i.'"><img src="'.$num.'" width="150" height="150"/></a>';
						    // print substr(substr($num,4,200),0,-4);
						    //$table .= '</td>';
						    $table .= '</div>';
						    if ($colCnt==1)
						        {
						        
						        $table .= '</tr>';
						        $colCnt=0;
						        }
						}
 */

